package easy;

public class RomanToInteger {
//note:can we use map to optimize it?
    public static int getInteger(char c){
        int i = 0;
        switch(c){
            case 'I':i = 1;break;
            case 'V':i = 5;break;
            case 'X':i = 10;break;
            case 'L':i = 50;break;
            case 'C':i = 100;break;
            case 'D':i = 500;break;
            case 'M':i = 1000;break;
            default: i = 0;break;
        }
        return i;
    }
    public static int romanToInt(String s) {
        int totalNum = 0;
        for(int i=0;i<s.length();i++){
            int tempNum = getInteger(s.charAt(i));
            if(i<s.length()-1) {
                int nextNum = getInteger(s.charAt(i + 1));
                if (nextNum > tempNum) {
                    tempNum = nextNum - tempNum;
                    i++;
                }
            }
            totalNum+=tempNum;
        }
        return totalNum;
    }
//    public static  void  main(String[]  args)  {
//        String ss = "DCXXI";
//        romanToInt(ss);
//    }
}
